<template>
  <div class="order-send">
    <t-dialog
      header="发货"
      :visible.sync="visible"
      @confirm="onConfirm"
      width="40%"
      :confirmOnEnter="true"
      :onClose="close"
    >
      <t-form :data="formData" :rules="rules" ref="form" label-align="top" @submit="onSubmit">
        <t-form-item label="发货方式" name="send_type">
          <t-radio-group v-model="formData.send_type" @change="changeSendType">
            <t-radio :value="0">自提</t-radio>
            <t-radio :value="1">快递</t-radio>
            <t-radio :value="2">跑腿</t-radio>
          </t-radio-group>
        </t-form-item>
        <t-form-item v-if="formData.send_type === 1" label="快递名称" name="delivery_name">
          <t-input v-model="formData.delivery_name" placeholder="请输入快递名称"></t-input>
        </t-form-item>
        <t-form-item v-if="formData.send_type === 1" label="物流单号" name="delivery_no">
          <t-input v-model="formData.delivery_no" placeholder="请输入物流单号"></t-input>
        </t-form-item>
      </t-form>
    </t-dialog>
  </div>
</template>

<script>
import {agentOrderSendApi} from "@/api/order";

export default {
  name: 'OrderSend',
  props: {
    info: {
      type: Object,
      default: ()=> {}
    }
  },
  data() {
    return {
      visible: false,
      formData: {
        send_type: 0, // 发货方式
        delivery_no: '', // 快递单号
        delivery_name: '', // 备注描述
        stock_data: [], // 商品信息
      },
      rules: {
        send_type: [{ required: true, message: '请选择发货方式', type: 'error' }],
        delivery_name: [{ required: false, message: '请输入快递名称', type: 'error' }],
        delivery_no: [{ required: true, message: '请输入快递单号', type: 'error' }],
      }
    }
  },
  methods: {
    changeSendType(value) {
      if (value === 1) {
        this.rules.delivery_name[0].required = true
      } else {
        this.rules.delivery_name[0].required = false
      }
    },
    onConfirm() {
      this.$refs.form.submit()
    },
    onSubmit({ validateResult, firstError }) {
      if (validateResult === true) {
        this.handleSubmit()
      } else {
        this.$message.warning(firstError);
      }
    },
    handleSubmit() {
      agentOrderSendApi(this.info.id, this.formData).then(res=> {
        this.$message.success('发货成功')
        this.$emit('refresh')
        this.close()
      }).catch(err=> {
        console.log('err', err)
      })
    },
    open(lists) {
      this.formData.stock_data = JSON.stringify(lists)
      this.visible = true
    },
    close() {
      this.visible = false
    }
  }
}
</script>

<style lang="less" scoped>
  .order-send {

  }
</style>
